package com.rtu.web;

import com.alibaba.fastjson.JSONObject;
import com.rtu.dto.QueryReportDto;
import com.rtu.dto.QueryRtuDto;
import com.rtu.dto.QuerySiphonDto;
import com.rtu.dto.RtuDto;
import com.rtu.dto.response.ApiResponse;
import com.rtu.enums.Code;
import com.rtu.enums.SDMappingEnum;
import com.rtu.service.IRtuService;
import com.rtu.service.impl.RtuServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by ljf on 2018/4/15.
 */
@Controller
@RequestMapping("/report")
public class RtuController {
    private static final Logger logger = LoggerFactory.getLogger(RtuServiceImpl.class);
    @Resource
    private IRtuService rtuService;

    @RequestMapping("/getList")
    @ResponseBody
    public ApiResponse getList(QueryRtuDto dto){
        ApiResponse apiResponse;
        try {
            if (dto == null || dto.getAreaId() == null) {
                apiResponse = ApiResponse.create(false,null, Code.BAD_PARAMS.code,null);
                return apiResponse;
            }
            String deviceId = SDMappingEnum.getDeviceId(dto.getAreaId());
            if (deviceId == null) {
                apiResponse = ApiResponse.create(false,null, Code.BAD_PARAMS.code,null);
                return apiResponse;
            } else {
                dto.setDeviceId(deviceId);
            }
            if (dto.getPage() == null) {
                dto.setPage(1);
            }
            if (dto.getPageRowCount() == null) {
                dto.setPageRowCount(50);
            }
            Map map = rtuService.query(dto);
            apiResponse = ApiResponse.create(true, Code.SUCCESS,map);
        }catch (Exception e){
            logger.error("getList",e);
            apiResponse = ApiResponse.create(false,e.getMessage(), Code.COMMON_SERVER_ERROR.code,null);
        }
        return apiResponse;
    }

    @RequestMapping("/getRecentData")
    @ResponseBody
    public ApiResponse getRecentData(QueryRtuDto dto, HttpServletRequest request, HttpServletResponse response){
        ApiResponse apiResponse;
        try {
            if (dto == null || dto.getAreaId() == null) {
                apiResponse = ApiResponse.create(false,null, Code.BAD_PARAMS.code,null);
                return apiResponse;
            }
            String deviceId = SDMappingEnum.getDeviceId(dto.getAreaId());
            if (deviceId == null) {
                apiResponse = ApiResponse.create(false,null, Code.BAD_PARAMS.code,null);
                return apiResponse;
            } else {
                dto.setDeviceId(deviceId);
            }
            List<RtuDto> list = rtuService.queryRecent(dto);
            apiResponse = ApiResponse.create(true, Code.SUCCESS,list);
        }catch (Exception e){
            logger.error("getRecentData",e);
            apiResponse = ApiResponse.create(false,e.getMessage(),Code.COMMON_SERVER_ERROR.code,null);
        }
        return apiResponse;
    }

    @RequestMapping("/getReportData")
    @ResponseBody
    public ApiResponse getReportData(QueryReportDto dto, HttpServletRequest request, HttpServletResponse response){
        logger.info("getReportData request={}", JSONObject.toJSONString(dto));
        ApiResponse apiResponse;
        try {
            if (dto == null || dto.getAreaId() == null || dto.getBeginTime() == null || dto.getEndTime() == null) {
                apiResponse = ApiResponse.create(false,null, Code.BAD_PARAMS.code,null);
                return apiResponse;
            }
            String deviceId = SDMappingEnum.getDeviceId(dto.getAreaId());
            if (deviceId == null) {
                apiResponse = ApiResponse.create(false,null, Code.BAD_PARAMS.code,null);
                return apiResponse;
            } else {
                dto.setDeviceId(deviceId);
            }
            Map map = rtuService.queryDataForReport(dto);
            apiResponse = ApiResponse.create(true, Code.SUCCESS,map);
        }catch (Exception e){
            logger.error("getReportData",e);
            apiResponse = ApiResponse.create(false,e.getMessage(),Code.COMMON_SERVER_ERROR.code,null);
        }
        return apiResponse;
    }
    /*
     * DeviceId = 18000004
     *  AreaId = 12
     */
    
    @RequestMapping("/getSiphonList")
    @ResponseBody
    public ApiResponse getSiphonList(QuerySiphonDto dto){
        ApiResponse apiResponse;
        try {
            if (dto == null || dto.getAreaId() == null) {
                apiResponse = ApiResponse.create(false,null, Code.BAD_PARAMS.code,null);
                return apiResponse;
            }
            String deviceId = SDMappingEnum.getDeviceId(dto.getAreaId());
  
            if (deviceId == null) {
                apiResponse = ApiResponse.create(false,null, Code.BAD_PARAMS.code,null);
                return apiResponse;
            } else {
            	deviceId="18000004";
                dto.setDeviceId(deviceId);
                dto.setCh("0");
            }
            if (dto.getPage() == null) {
                dto.setPage(1);
            }
            if (dto.getPageRowCount() == null) {
                dto.setPageRowCount(50);
            }
            Map map = rtuService.querySiphon(dto);
            apiResponse = ApiResponse.create(true, Code.SUCCESS,map);
        }catch (Exception e){
            logger.error("getList",e);
            apiResponse = ApiResponse.create(false,e.getMessage(), Code.COMMON_SERVER_ERROR.code,null);
        }
        return apiResponse;
    }
    
    /*
     * DeviceId = 18000004  ch=1
     *  AreaId = 12
     *  year statistic data 
     */   
    @RequestMapping("/getYearSiphonList")
    @ResponseBody
    public ApiResponse getYearSiphonList(QuerySiphonDto dto){
        ApiResponse apiResponse;
        try {
            if (dto == null || dto.getAreaId() == null) {
                apiResponse = ApiResponse.create(false,null, Code.BAD_PARAMS.code,null);
                return apiResponse;
            }
            String deviceId = SDMappingEnum.getDeviceId(dto.getAreaId());
  
            if (deviceId == null) {
                apiResponse = ApiResponse.create(false,null, Code.BAD_PARAMS.code,null);
                return apiResponse;
            } else {
            	deviceId="18000004";
                dto.setDeviceId(deviceId);
                dto.setCh("0");               
            }
            if (dto.getPage() == null) {
                dto.setPage(1);
            }
            if (dto.getPageRowCount() == null) {
                dto.setPageRowCount(50);
            }
            Map map = rtuService.queryYearSiphon(dto);
            apiResponse = ApiResponse.create(true, Code.SUCCESS,map);
        }catch (Exception e){
            logger.error("getList",e);
            apiResponse = ApiResponse.create(false,e.getMessage(), Code.COMMON_SERVER_ERROR.code,null);
        }
        return apiResponse;
    }
    
    /*
     * DeviceId = 18000008 ch=1
     *  AreaId = 12
     */
    @RequestMapping("/getSpillwayList")
    @ResponseBody
    public ApiResponse getSpillwayList(QuerySiphonDto dto){
        ApiResponse apiResponse;
        try {
            if (dto == null || dto.getAreaId() == null) {
                apiResponse = ApiResponse.create(false,null, Code.BAD_PARAMS.code,null);
                return apiResponse;
            }
            String deviceId = SDMappingEnum.getDeviceId(dto.getAreaId());
        
            if (deviceId == null) {
                apiResponse = ApiResponse.create(false,null, Code.BAD_PARAMS.code,null);
                return apiResponse;
            } else {
            	deviceId="18000008";
                dto.setDeviceId(deviceId);
                dto.setCh("1");
            }
            if (dto.getPage() == null) {
                dto.setPage(1);
            }
            if (dto.getPageRowCount() == null) {
                dto.setPageRowCount(50);
            }
            Map map = rtuService.querySpillway(dto);
            apiResponse = ApiResponse.create(true, Code.SUCCESS,map);
        }catch (Exception e){
            logger.error("getList",e);
            apiResponse = ApiResponse.create(false,e.getMessage(), Code.COMMON_SERVER_ERROR.code,null);
        }
        return apiResponse;
    }
    
    
    @RequestMapping("/getYearSpillwayList")
    @ResponseBody
    public ApiResponse getYearSpillwayList(QuerySiphonDto dto){
        ApiResponse apiResponse;
        try {
            if (dto == null || dto.getAreaId() == null) {
                apiResponse = ApiResponse.create(false,null, Code.BAD_PARAMS.code,null);
                return apiResponse;
            }
            String deviceId = SDMappingEnum.getDeviceId(dto.getAreaId());
  
            if (deviceId == null) {
                apiResponse = ApiResponse.create(false,null, Code.BAD_PARAMS.code,null);
                return apiResponse;
            } else {
            	
                dto.setDeviceId("18000008");
                dto.setCh("1");
            }
            if (dto.getPage() == null) {
                dto.setPage(1);
            }
            if (dto.getPageRowCount() == null) {
                dto.setPageRowCount(50);
            }
            Map map = rtuService.queryYearSpillway(dto);
            apiResponse = ApiResponse.create(true, Code.SUCCESS,map);
        }catch (Exception e){
            logger.error("getList",e);
            apiResponse = ApiResponse.create(false,e.getMessage(), Code.COMMON_SERVER_ERROR.code,null);
        }
        return apiResponse;
    }
    
    
}
